//
//  HZTextFieldWithAccessoryView.m
//  HZTC_YaDeArt
//
//  Created by MayerF on 17/4/28.
//  Copyright © 2017年 HZTC. All rights reserved.
//

#import "HZTextFieldWithAccessoryView.h"

@interface HZTextFieldWithAccessoryView()<UITextFieldDelegate>
@property (nonatomic, strong)UIView *pAccessoryView;
@property (nonatomic, strong)UILabel *pTextLabel;
@property (nonatomic, strong)UIButton *pHiddenBtn;
@property (nonatomic, strong)UIButton *pClearBtn;
@end

@implementation HZTextFieldWithAccessoryView

- (void)layoutSubviews{
    [super layoutSubviews];
//    _pClearBtn.frame = CGRectMake(0, 0, 50, CGRectGetHeight(self.frame));
}

- (instancetype)init{
    return [self initWithFrame:CGRectZero];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self p_commonInit];
    }
    return self;
}

- (void)p_commonInit
{
    _pAccessoryView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 30)];
    _pTextLabel = [UILabel new];
    _pHiddenBtn = [UIButton new];
    
    _pAccessoryView.layer.borderWidth = 0.5;
    _pAccessoryView.layer.borderColor = kMYHexToRGB(0xc8c7cc, 1).CGColor;
    _pAccessoryView.backgroundColor = kMYHexToRGB(0xf5f2ed, 1);
    _pTextLabel.textAlignment = NSTextAlignmentCenter;
    _pTextLabel.textColor = kMYHexToRGB(0xf15059, 1);
    [_pHiddenBtn setTitle:@"隐藏" forState:UIControlStateNormal];
    [_pHiddenBtn setTitleColor:kMYHexToRGB(0xf15059, 1) forState:UIControlStateNormal];
    [_pHiddenBtn addTarget:self action:@selector(hiddenBtnOnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _pClearBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 30)];
    [_pClearBtn setImage:[UIImage imageNamed:@"clear"] forState:UIControlStateNormal];
    [_pClearBtn addTarget:self action:@selector(clearOnClick) forControlEvents:UIControlEventTouchUpInside];
    self.rightView = _pClearBtn;
    self.rightViewMode = UITextFieldViewModeWhileEditing;
    
    self.inputAccessoryView = _pAccessoryView;
    self.delegate = self;
    
    [_pAccessoryView addSubview:_pTextLabel];
    [_pAccessoryView addSubview:_pHiddenBtn];
    
    [self p_initConstraints];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChange) name:UITextFieldTextDidChangeNotification object:self];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keboardWillShow:) name:UIKeyboardWillShowNotification object:self];
}

- (void)p_initConstraints
{
    [_pTextLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pAccessoryView);
        make.left.mas_equalTo(_pAccessoryView);
        make.right.mas_equalTo(_pHiddenBtn.mas_left);
        make.bottom.mas_equalTo(_pAccessoryView);
    }];
    [_pHiddenBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_pAccessoryView);
        make.right.mas_equalTo(_pAccessoryView);
        make.bottom.mas_equalTo(_pAccessoryView);
        make.width.mas_equalTo(50);
    }];
}
#pragma mark -
#pragma mark action
- (void)clearOnClick
{
    self.text = @"";
    _pTextLabel.text = @"";
}

- (void)hiddenBtnOnClick
{
    [self resignFirstResponder];
}

#pragma mark -
#pragma mark notification
- (void)textFieldChange
{
    _pTextLabel.text = self.text;
}

- (void)keboardWillShow:(NSNotification *)noti
{
    NSDictionary* keyboardInfo = [noti userInfo];
    CGRect keyboardFrame = [keyboardInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    _pAccessoryView.frame = CGRectMake(0, 0, keyboardFrame.size.width, 30);
}

#pragma mark -
#pragma mark UITextFieldDelegate
- (BOOL)textFieldShouldClear:(UITextField *)textField{
    _pTextLabel.text = self.text;
    return YES;
}

- (void)dealloc{
    DLog(@"accessoryView");
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
